/*
 * @Author: Wushiqi
 * @Descripttion: 封装axios及拦截器配置
 * @Date: 2020-07-26 16:35:13
 * @LastEditors: Wushiqi
 * @LastEditTime: 2020-09-01 23:42:50
 */
import axios from 'axios'
import router from '@/router/index'
import Vue from 'vue'

const http = axios.create({
  baseURL: 'http://112.74.99.5:3000/web/api'
})

// 请求拦截器
http.interceptors.request.use(function (config) {
  // console.log(config);
  if (localStorage.getItem('token') && localStorage.getItem('id')) {
    config.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
  }
  return config;
}, function (error) {
  return Promise.reject(error);
});

// 响应拦截器
http.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  if (error.response.status === 401 || error.response.status === 402) {
    router.push('/login')
    Vue.prototype.$msg.fail(error.response.data.message)
  }
  return Promise.reject(error);
});
export default http